package com.minivision.sdktestdemo.theme.ui;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.minivision.sdktestdemo.theme.utils.CustomThemeInterface;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;
import com.minivision.sdktestdemo.theme.utils.ViewAttributeUtils;

/**
 * Created by Administrator on 2016/6/21.
 */
public class CustomThemeTextView extends TextView implements CustomThemeInterface {
    private int attrTextColor = -1;

    public CustomThemeTextView(Context context) {
        super(context);
    }

    public CustomThemeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        attrTextColor = ViewAttributeUtils.getTextColorAttribute(attrs);
    }

    public CustomThemeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        attrTextColor = ViewAttributeUtils.getTextColorAttribute(attrs);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            if (ViewAttributeUtils.isNeedChangeColor(this)) {
                setTextColor(ThemeUtils.getThemeColor());
            } else {
                setTextColor(Color.RED);
            }
        } else {
            ViewAttributeUtils.applyTextColor(this, ThemeUtils.getTheme(), attrTextColor);
        }
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void setTheme(Resources.Theme themeId) {
        if (attrTextColor != -1) {
            if (ViewAttributeUtils.isNeedChangeColor(this)) {
            } else {
                ViewAttributeUtils.applyTextColor(this, themeId, attrTextColor);
            }
        }
    }
}
